package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;
import com.cloudsigns.factorymanager2.engineering.presenter.MediaListByIdPresenter;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

/**
 * 机场平面地图
 * Created by tony on 2018/5/11.
 */

public class RegionMapForSuperActivity extends Activity implements View.OnClickListener {
    private RelativeLayout rl_right;
    private ImageView img_right;
    private TextView tv_title;
    private View rl_back;
    private ImageView img_region_bg;
    private DropDownPoupWindow popupWindow;
    private final String TAG = RegionMapForSuperActivity.class.getSimpleName();
    /*[ [项目部-媒体库]根据id获取媒体列表 ]*/
    private MediaListByIdPresenter mediaListByIdPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_region_map_for_super);

        init();
        initData();
    }

    private void init() {
        rl_right = findViewById(R.id.rl_right);
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        img_right = findViewById(R.id.img_right);
        img_region_bg = findViewById(R.id.img_region_bg);

        img_right.setBackgroundResource(R.mipmap.title_icon_menu);
        rl_right.setVisibility(View.VISIBLE);
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);


    }

    private void initData() {
        Intent mIntent = getIntent();
        if (null != mIntent) {
            RegionBean regionBean = (RegionBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
            if (null != regionBean)
                tv_title.setText(regionBean.getRegionName());
        }

        Glide.with(this).load(R.mipmap.region_bg).asBitmap().into(img_region_bg);
    }


    /**
     * 设置添加屏幕的背景透明�?
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    private void showPopupWindow(View anchor) {
        popupWindow = new DropDownPoupWindow(RegionMapForSuperActivity.this, TAG,0, R.layout.popwindow_map_region_list_for_super, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        backgroundAlpha(0.4f);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);

            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_right:
                showPopupWindow(view);
                break;
            case R.id.rl_back:
                finish();
                break;


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

}
